#include "logger.h"

#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <memory>

using namespace std;

static const char LOG_TYPE_DESC[] = "EWIDCCCCCCCCCCCCCCCCCCCCCC";

Logger &Logger::Instance() {
  static unique_ptr<Logger> _iIns;
  if (!_iIns.get())
    _iIns.reset(new Logger);
  return *(_iIns.get());
}

void Logger::Log(ELog::Level emLevel, const char *pFmt, ...) {
  std::unique_lock<std::mutex> iAuto(_iLock);
  time_t nNow = time(NULL);
  struct tm *pLocal = nullptr;

#if defined(_WIN32)
  struct tm iLocal = {0};
  pLocal = &iLocal;
  localtime_s(pLocal, &nNow);
#else
  pLocal = localtime(&nNow);
#endif
  printf("[%04d-%02d-%02d %02d:%02d:%02d][%c] ", pLocal->tm_year + 1900,
         pLocal->tm_mon + 1, pLocal->tm_mday, pLocal->tm_hour, pLocal->tm_min,
         pLocal->tm_sec, LOG_TYPE_DESC[emLevel]);

  va_list args;
  va_start(args, pFmt);
  vprintf(pFmt, args);
  printf("\n");
  va_end(args);
}
